package app.dao;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;

import app.models.Animal;
import br.com.caelum.vraptor.ioc.Component;

@Component
public class AnimalDao {

	private EntityManager manager;

	AnimalDao (EntityManager manager){
		this.manager = manager;
	}

	public Animal loadByNome(String nome) throws Exception {
		try {
			Query query = manager.createQuery("from Animal where nome = :nome");
			query.setParameter("nome", nome);
			Animal animal = (Animal) query.getSingleResult();
			return animal;
		} catch (NoResultException e) {
			manager.getTransaction().rollback();
			throw new Exception("Nenhum resultado!");
		} catch (Exception e) {
			manager.getTransaction().rollback();
			throw new Exception("Ocorreu um erro na busca!");
		}
	}

	public void salva(Animal animal) {
		// TODO Auto-generated method stub
		
	}

}
